package com.erp.order.purchasing.controller;

import com.erp.hr.base.controller.BaseController;
import com.erp.hr.base.vo.ResultVO;
import com.erp.order.purchasing.model.PurchasingAudit;
import com.erp.order.purchasing.model.PurchasingRequisition;
import com.erp.order.purchasing.service.PurchasingAuditService;
import org.redragon.framework.hibernate.model.Pages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;

/**
 * 采购审核 控制层
 * @Author: syz
 * @blame syz
 * @CreateTime: 2021/4/6
 */
@Controller
@RequestMapping("web/purchasingAudit")
public class PurchasingAuditController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PurchasingAuditController.class);

    /**
     * 采购审核服务
     */
    @Autowired
    private PurchasingAuditService purchasingAuditService;


    /**
     * 跳转至库管审核界面
     * @return 页面路径
     */
    @RequestMapping("show")
    public String show() {
        return "basic.jsp?content=purchasingAudit/list";
    }

    /**
     * 获取采购计划列表分页数据
     * @param pages 分页对象
     * @param purchasingAudit 采购审核对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("getMainPageList")
    public String getMainPageList(Pages pages, PurchasingAudit purchasingAudit, Model model) {
        model.addAttribute("pages", setPages(pages));
        model.addAttribute("list", purchasingAuditService.addCompleteData(purchasingAuditService.getDataObjectsForDataAuth("", pages, purchasingAudit)));
        model.addAttribute("typeMap", purchasingAuditService.getDictByKey("audit_type"));
        model.addAttribute("statusMap", purchasingAuditService.getDictByKey("requisition_status"));
        return "purchasingAudit/tab/main/list";
    }

    /**
     * 新增/编辑
     * @param purchasingAudit 需要新增/编辑的对象
     * @return 新增/编辑结果
     */
    @ResponseBody
    @RequestMapping("save")
    public ResultVO<String> save(@Valid @RequestBody PurchasingAudit purchasingAudit) {
        return saveOrUpdate(purchasingAuditService, purchasingAudit);
    }

    /**
     * 跳转至详情页
     * @param purchasingAudit 采购计划对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("detail")
    public String detail(PurchasingAudit purchasingAudit, Model model) {
        model.addAttribute("purchasingAudit",
                purchasingAuditService.addCompleteData(
                        purchasingAuditService.getDataObject(
                                purchasingAudit.getCode())));
        model.addAttribute("typeMap", purchasingAuditService.getDictByKey("audit_type"));
        model.addAttribute("subTypeMap", purchasingAuditService.getDictByKey("requisition_type"));
        model.addAttribute("statusMap", purchasingAuditService.getDictByKey("requisition_status"));
        return "basic.jsp?content=purchasingAudit/tab/main/detail";
    }

    /**
     * 获取可合并列表分页数据
     * @param pages 分页对象
     * @param purchasingRequisition 采购申请对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("getSubPageList")
    public String getSubPageList(Pages pages, PurchasingRequisition purchasingRequisition, Model model) {
        model.addAttribute("pages", setPages(pages));
        model.addAttribute("list", purchasingAuditService.getSubPageList("", pages, purchasingRequisition));
        model.addAttribute("typeMap", purchasingAuditService.getDictByKey("requisition_type"));
        model.addAttribute("statusMap", purchasingAuditService.getDictByKey("requisition_status"));
        return "purchasingAudit/tab/sub/list";
    }

    /**
     * 获取申请数据
     * @param purchasingRequisition 申请对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("getSubEdit")
    public String getSubEdit(PurchasingRequisition purchasingRequisition, Model model) {
        model.addAttribute("purchasingRequisition", purchasingAuditService.getSub(purchasingRequisition.getId().toString()));
        model.addAttribute("typeMap", purchasingAuditService.getDictByKey("requisition_type"));
        model.addAttribute("unitMap", purchasingAuditService.getDictByKey("material_unit"));
        model.addAttribute("subStatusMap", purchasingAuditService.getDictByKey("requisition_detail_status"));
        return "basic.jsp?content=purchasingAudit/tab/sub/edit";
    }

}